FROM debian:13.1-slim
ADD mosquitto-2.0.22.tar.gz /usr/local/
WORKDIR /usr/local/mosquitto-2.0.22
RUN sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list.d/debian.sources && \
apt-get -y update && \
apt-get -y install build-essential libc-ares-dev libc6-dev libssl-dev libwebsockets-dev libcjson-dev && \
make WITH_WEBSOCKETS=yes WITH_SRV=yes && make install && \
apt purge -y build-essential && \
apt -y autoremove && \
rm -rf /var/lib/apt/lists/* && \
rm -rf /usr/local/mosquitto-2.0.22 && \
cp /etc/mosquitto/mosquitto.conf.example /etc/mosquitto/mosquitto.conf && \
# 创建密码文件
# 用户名称mosquitto密码mosquitto
mosquitto_passwd -c -b /etc/mosquitto/password_file mosquitto mosquitto && \
groupadd mosquitto && \
useradd mosquitto -g mosquitto && \
chown -R mosquitto:mosquitto /etc/mosquitto/ && \
# 配置认证
# 默认启动用户mosquitto配置文件/etc/mosquitto/password_file
echo "password_file /etc/mosquitto/password_file" >>/etc/mosquitto/mosquitto.conf && \
# 设置外网访问
echo "listener 1883 0.0.0.0" >>/etc/mosquitto/mosquitto.conf
#CMD ["mosquitto","-c","/etc/mosquitto/mosquitto.conf","-d"]
CMD ["mosquitto"]

# https://github.com/eclipse-mosquitto/mosquitto
# docker build -t registry.cn-hangzhou.aliyuncs.com/xiaoyilin/mosquitto:2.0.22 .
# docker run --rm -it registry.cn-hangzhou.aliyuncs.com/xiaoyilin/mosquitto:2.0.22 bash
# docker run -p 1883:1883 registry.cn-hangzhou.aliyuncs.com/xiaoyilin/mosquitto:2.0.22

# mosquitto is an MQTT v5.0/v3.1.1/v3.1 broker
# Usage: mosquitto [-c config_file] [-d] [-h] [-p port]
# -c : 指定配置文件
# -d : 后台运行
# -p : 指定端口(不建议与-c选项结合使用).
# -v : 详细模式-启用所有日志记录类型(将覆盖配置文件中给出的任何日志记录选项)
# https://mosquitto.org/man/mosquitto-conf-5.html
# per_listener_settings true为每个侦听器设置单独的安全设置，则必须将密码文件选项放在它所用于的侦听器之后